/**
 * 生成品牌LOGO占位图SVG
 * @param {string} brandName - 品牌名称
 * @returns {string} - Data URL格式的SVG图片
 */
export const generateLogoPlaceholder = (brandName = 'LOGO') => {
  // 转义品牌名中的特殊字符，防止XML解析错误
  const safeBrandName = String(brandName || 'LOGO')
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&#39;')

  // 计算文字大小，根据字符数自动调整
  const fontSize = Math.max(8, Math.min(20, 24 - safeBrandName.length * 1.5))

  // 新的占位图设计，包含网格线和角标切线
  const svg = `<svg width="128" height="128" viewBox="0 0 128 128" role="img" aria-label="Logo placeholder" xmlns="http://www.w3.org/2000/svg">
    <rect x="0.5" y="0.5" width="127" height="127" rx="16" fill="#f6f7f9" stroke="#d9dee5"/>
    <g stroke="#d9dee5" stroke-width="1">
      <line x1="64" y1="0" x2="64" y2="128" opacity="0.5"/>
      <line x1="0" y1="64" x2="128" y2="64" opacity="0.5"/>
      <line x1="32" y1="0" x2="32" y2="128" opacity="0.25"/>
      <line x1="96" y1="0" x2="96" y2="128" opacity="0.25"/>
      <line x1="0" y1="32" x2="128" y2="32" opacity="0.25"/>
      <line x1="0" y1="96" x2="128" y2="96" opacity="0.25"/>
    </g>
    <circle cx="64" cy="64" r="28" fill="none" stroke="#d9dee5" stroke-width="2"/>
    <path d="M8 8h12M8 8v12M120 8h-12M120 8v12 M8 120h12M8 120v-12M120 120h-12M120 120v-12" stroke="#d9dee5" stroke-width="1.5" stroke-linecap="round" opacity="0.7"/>
    <text x="64" y="72" text-anchor="middle" font-family="system-ui, -apple-system, Segoe UI, Roboto, Arial" font-size="${fontSize}" fill="#6b7785" style="font-weight:700; letter-spacing:1px">${safeBrandName}</text>
  </svg>`

  // 使用 encodeURIComponent 和 unescape 来处理非Latin1字符
  const encodedSvg = encodeURIComponent(svg)
  const decodedSvg = unescape(encodedSvg)
  return `data:image/svg+xml;base64,${btoa(decodedSvg)}`
}

/**
 * 获取默认LOGO占位图
 * @returns {string} - 默认的LOGO占位图Data URL
 */
export const getDefaultLogo = () => {
  return generateLogoPlaceholder('LOGO')
}
